package com.subang.function.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.framework.web.service.UserIntegralService;
import com.subang.function.domain.SysUserCollect;
import com.subang.function.service.ISysUserCollectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbArticle;
import com.subang.function.service.ISbArticleService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 文章Controller
 * 
 * @author subei
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/function/article")
public class SbArticleController extends BaseController
{
    @Autowired
    private ISbArticleService sbArticleService;

    @Autowired
    private ISysUserCollectService sysUserCollectService;

    @Autowired
    private UserIntegralService userIntegralService;

    /**
     * 查询文章列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SbArticle sbArticle)
    {
        startPage();
        List<SbArticle> list;
        String orderBy = sbArticle.getOrderBy();
        Long isRecommend = sbArticle.getIsRecommend();
        Long myCollect = sbArticle.getMyCollect();
        if(("createTime").equals(orderBy)){
            list = sbArticleService.selectSbArticleListOrderByCreateTime(sbArticle);
        } else if (("viewCount").equals(orderBy)) {
            list = sbArticleService.selectSbArticleListOrderByViewCount(sbArticle);
        }else if(isRecommend != null && 1L == isRecommend){
            sbArticle.setFeatured(1L);
            list = sbArticleService.selectSbArticleList(sbArticle);
        }else if(myCollect != null && 1L == myCollect){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            SysUserCollect sysUserCollect = new SysUserCollect();
            sysUserCollect.setUserId(user.getUserId());
            if(sbArticle.getChannelId()==0){
                sysUserCollect.setCollectType("上岸经验");
            }else if(sbArticle.getChannelId()==5){
                sysUserCollect.setCollectType("苏榜原创");
            }
            List<SysUserCollect> myCollectList = sysUserCollectService.selectSysUserCollectList(sysUserCollect);
            List<Long> articleIds = new ArrayList<>();
            for (SysUserCollect collect : myCollectList) {
                articleIds.add(collect.getCollectId());
            }
            list = sbArticleService.selectSbArticleListByIds(articleIds);
        }else{
            list = sbArticleService.selectSbArticleList(sbArticle);
        }
        return getDataTable(list);
    }

    @GetMapping("/list/{channelId}")
    public TableDataInfo list(SbArticle sbArticle,@PathVariable("channelId") Long channelId,@PathVariable("orderBy") String orderBy)
    {
        startPage();
        sbArticle.setChannelId(channelId);
        List<SbArticle> list;
        if(orderBy.equals("createTime")){
            list = sbArticleService.selectSbArticleListOrderByCreateTime(sbArticle);
        } else if (orderBy.equals("viewCount")) {
            list = sbArticleService.selectSbArticleListOrderByViewCount(sbArticle);
        }else{
            sbArticle.setFavors(1L);
            list = sbArticleService.selectSbArticleList(sbArticle);
        }


        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('function:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbArticle sbArticle)
    {
        List<SbArticle> list = sbArticleService.selectSbArticleList(sbArticle);
        ExcelUtil<SbArticle> util = new ExcelUtil<SbArticle>(SbArticle.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        if (success(sbArticleService.selectSbArticleById(id)).isSuccess()) {
            return userIntegralService.updateUserIntegral("viewArticle");
        } else {
            return AjaxResult.error("查看文章失败");
        }
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('function:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbArticle sbArticle)
    {
        return toAjax(sbArticleService.insertSbArticle(sbArticle));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('function:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbArticle sbArticle)
    {
        return toAjax(sbArticleService.updateSbArticle(sbArticle));
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('function:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sbArticleService.deleteSbArticleByIds(ids));
    }
}
